class_name PlayerController extends Node

# 玩家状态
var player_name = "冒险者"
var player_level = 1
var player_experience = 0
var player_experience_to_next_level = 100

# 玩家位置
var current_area = "村庄"
var position_x = 0
var position_y = 0

# 玩家幻兽队伍
var monster_team = []
var active_monster_index = 0
var max_team_size = 6

# 玩家背包
var inventory = {
    "gold": 500,
    "items": {},
    "key_items": {}
}

# 玩家任务
var quests = {
    "active": [],
    "completed": []
}

# 玩家进度
var progress = {
    "areas_visited": ["村庄"],
    "bosses_defeated": [],
    "story_flags": {}
}

# 信号
signal monster_team_changed()
signal inventory_changed()
signal player_moved(area, x, y)
signal player_leveled_up(new_level)

# 初始化玩家
func initialize(name = "冒险者"):
    player_name = name
    print("初始化玩家: %s" % player_name)
    return self

# 添加初始幻兽
func add_starter_monster(monster):
    if monster_team.size() < max_team_size:
        monster_team.append(monster)
        active_monster_index = 0
        print("%s 获得了初始幻兽: %s (Lv.%d)" % [player_name, monster.monster_name, monster.level])
        emit_signal("monster_team_changed")
        return true
    else:
        print("错误: 幻兽队伍已满")
        return false

# 添加幻兽到队伍
func add_monster_to_team(monster):
    if monster_team.size() < max_team_size:
        monster_team.append(monster)
        print("%s 将 %s (Lv.%d) 加入了队伍" % [player_name, monster.monster_name, monster.level])
        emit_signal("monster_team_changed")
        return true
    else:
        print("错误: 幻兽队伍已满")
        # 这里将来会实现幻兽存储系统
        return false

# 移除幻兽从队伍
func remove_monster_from_team(index):
    if index >= 0 and index < monster_team.size():
        var monster = monster_team[index]
        monster_team.remove(index)
        
        # 调整当前活跃幻兽索引
        if active_monster_index >= monster_team.size():
            active_monster_index = max(0, monster_team.size() - 1)
        
        print("%s 将 %s (Lv.%d) 从队伍中移除" % [player_name, monster.monster_name, monster.level])
        emit_signal("monster_team_changed")
        return monster
    else:
        print("错误: 无效的幻兽索引")
        return null

# 切换活跃幻兽
func switch_active_monster(index):
    if index >= 0 and index < monster_team.size():
        active_monster_index = index
        var monster = monster_team[active_monster_index]
        print("%s 将 %s (Lv.%d) 设为主力幻兽" % [player_name, monster.monster_name, monster.level])
        emit_signal("monster_team_changed")
        return true
    else:
        print("错误: 无效的幻兽索引")
        return false

# 获取活跃幻兽
func get_active_monster():
    if monster_team.size() > 0:
        return monster_team[active_monster_index]
    else:
        print("警告: 幻兽队伍为空")
        return null

# 获取幻兽队伍
func get_monster_team():
    return monster_team

# 移动到新区域
func move_to_area(area_name, x = 0, y = 0):
    current_area = area_name
    position_x = x
    position_y = y
    
    if not area_name in progress.areas_visited:
        progress.areas_visited.append(area_name)
        print("%s 首次到达: %s" % [player_name, area_name])
    else:
        print("%s 移动到: %s (%d, %d)" % [player_name, area_name, x, y])
    
    emit_signal("player_moved", area_name, x, y)
    return true

# 添加物品到背包
func add_item(item_id, quantity = 1):
    if inventory.items.has(item_id):
        inventory.items[item_id] += quantity
    else:
        inventory.items[item_id] = quantity
    
    print("%s 获得了 %d 个 %s" % [player_name, quantity, item_id])
    emit_signal("inventory_changed")
    return true

# 移除物品从背包
func remove_item(item_id, quantity = 1):
    if inventory.items.has(item_id):
        if inventory.items[item_id] >= quantity:
            inventory.items[item_id] -= quantity
            
            # 如果数量为0，移除该物品
            if inventory.items[item_id] <= 0:
                inventory.items.erase(item_id)
            
            print("%s 失去了 %d 个 %s" % [player_name, quantity, item_id])
            emit_signal("inventory_changed")
            return true
        else:
            print("错误: 物品数量不足")
            return false
    else:
        print("错误: 背包中没有该物品")
        return false

# 使用物品
func use_item(item_id, target_monster_index = -1):
    if inventory.items.has(item_id) and inventory.items[item_id] > 0:
        # 如果目标索引为-1，则使用活跃幻兽
        if target_monster_index == -1:
            target_monster_index = active_monster_index
        
        if target_monster_index >= 0 and target_monster_index < monster_team.size():
            var target_monster = monster_team[target_monster_index]
            
            # 这里将来会实现物品效果系统
            print("%s 对 %s 使用了 %s" % [player_name, target_monster.monster_name, item_id])
            
            # 简单实现: 恢复药水恢复30点生命值
            if item_id == "healing_potion":
                target_monster.heal(30)
            
            # 使用后移除一个物品
            remove_item(item_id, 1)
            return true
        else:
            print("错误: 无效的目标幻兽索引")
            return false
    else:
        print("错误: 背包中没有该物品")
        return false

# 添加金币
func add_gold(amount):
    inventory.gold += amount
    print("%s 获得了 %d 金币" % [player_name, amount])
    emit_signal("inventory_changed")
    return true

# 移除金币
func remove_gold(amount):
    if inventory.gold >= amount:
        inventory.gold -= amount
        print("%s 失去了 %d 金币" % [player_name, amount])
        emit_signal("inventory_changed")
        return true
    else:
        print("错误: 金币不足")
        return false

# 添加经验值
func add_experience(amount):
    player_experience += amount
    print("%s 获得了 %d 点经验值" % [player_name, amount])
    
    # 检查是否可以升级
    while player_experience >= player_experience_to_next_level:
        player_experience -= player_experience_to_next_level
        level_up()
    
    return true

# 玩家升级
func level_up():
    player_level += 1
    player_experience_to_next_level = int(player_experience_to_next_level * 1.5)
    
    print("%s 升级到 %d 级!" % [player_name, player_level])
    emit_signal("player_leveled_up", player_level)
    return true

# 添加任务
func add_quest(quest_id, quest_data):
    if not quest_id in quests.active and not quest_id in quests.completed:
        quests.active.append(quest_id)
        print("%s 接受了任务: %s" % [player_name, quest_data.get("name", quest_id)])
        return true
    else:
        print("错误: 已经接受或完成了该任务")
        return false

# 完成任务
func complete_quest(quest_id):
    if quest_id in quests.active:
        quests.active.erase(quest_id)
        quests.completed.append(quest_id)
        print("%s 完成了任务: %s" % [player_name, quest_id])
        return true
    else:
        print("错误: 未接受该任务")
        return false

# 设置故事标记
func set_story_flag(flag_name, value = true):
    progress.story_flags[flag_name] = value
    print("设置故事标记: %s = %s" % [flag_name, str(value)])
    return true

# 检查故事标记
func check_story_flag(flag_name):
    return progress.story_flags.get(flag_name, false)

# 击败BOSS
func defeat_boss(boss_id):
    if not boss_id in progress.bosses_defeated:
        progress.bosses_defeated.append(boss_id)
        print("%s 击败了BOSS: %s" % [player_name, boss_id])
        return true
    else:
        print("警告: 已经击败过该BOSS")
        return false

# 获取玩家状态
func get_player_status():
    return {
        "name": player_name,
        "level": player_level,
        "experience": player_experience,
        "experience_to_next_level": player_experience_to_next_level,
        "current_area": current_area,
        "position": Vector2(position_x, position_y),
        "gold": inventory.gold,
        "monster_team_size": monster_team.size(),
        "active_monster": get_active_monster().get_status() if get_active_monster() else null,
        "areas_visited": progress.areas_visited,
        "bosses_defeated": progress.bosses_defeated
    }

# 保存游戏数据
func save_game_data():
    # 这里将来会实现存档系统
    print("保存游戏数据...")
    return true

# 加载游戏数据
func load_game_data():
    # 这里将来会实现读档系统
    print("加载游戏数据...")
    return true
